package com.xqkad.myfile.ui;

import com.xqkad.myfile.R;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MyAdapter extends BaseAdapter{
    private List<ItemData> mData;
    private Context mContext;
    private ListView lv;
    public static boolean cb_show = false;

    public MyAdapter(List<ItemData> data, Context context){
        this.mData = data;
        this.mContext = context;
        //this.lv = ((ListActivity)context).lv;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int i) {
        return mData.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    //int d=0;
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ItemView hd = null;
        if(view != null){
            hd = (ItemView) view.getTag();
        }else{
            view = View.inflate(mContext, R.layout.array_adapter, null);
            hd = new ItemView();
            hd.img = view.findViewById(R.id.iv_item);
            hd.tv = view.findViewById(R.id.tv_item);
            hd.cb = view.findViewById(R.id.cb_item);
            view.setTag(hd);
        }
        ItemData data = mData.get(i);
        hd.img.setImageResource(data.getImageid());
        hd.tv.setText(data.getText());
        hd.cb.setChecked(data.isCheck());
        //hd.cb.setChecked(lv.isItemChecked(i));
        if(cb_show) hd.cb.setVisibility(CheckBox.VISIBLE);
        else hd.cb.setVisibility(CheckBox.INVISIBLE);
        return view;
    }

    private static class ItemView{
        public ImageView img;
        public TextView tv;
        public CheckBox cb;
    }

    public void setItemCheck(int i, boolean b){
        mData.get(i).setCheck(b);
    }

    public void setAllCheck(boolean b){
        for(ItemData data : mData){
            data.setCheck(b);
        }
    }

    public void setData(List<ItemData> data){
        this.mData = data;
    }
}
